const path = require('path')

const {
 fileUploadError, unSupportedFileType,
 publishGoodsError, invalidGoodsID
} = require('../constant/error.type')
const {
 createGoods, updateGoods,
 removeGoods, restoreGoods,
 findGoods
} = require('../service/goods.service')

class GoodsController {
 async upload(ctx) {
  const { file } = ctx.request.files
  const fileTypes = ['image/jpeg', 'image/png']
  if (file) {
   if (!fileTypes.includes(file.type)) {
    return ctx.app.emit('error', unSupportedFileType, ctx)
   }
   ctx.body = {
    code: 0,
    message: '图片上传成功',
    result: {
     goods_img: path.basename(file.path)
    }
   }
  } else {
   return ctx.app.emit('error', fileUploadError, ctx)
  }
 }
 async create(ctx) {
  try {
   const { createdAt, updatedAt, ...res } = await createGoods(ctx.request.body)
   ctx.body = {
    code: 0,
    message: '商品发布成功',
    result: res
   }
  } catch (err) {
   console.error(err)
   return ctx.app.emit('error', publishGoodsError, ctx)
  }
 }
 async update(ctx) {
  try {
   const res = await updateGoods(ctx.params.id, ctx.request.body)
   if (res) {
    ctx.body = {
     code: 0,
     message: '修改商品成功',
     result: ''
    }
   } else {
    return ctx.app.emit('error', invalidGoodsID, ctx)
   }
  } catch (err) {
   console.error(err)
  }
  ctx.body = 'change goods success'
 }
 async remove(ctx) {
  const res = await removeGoods(ctx.params.id)
  if (res) {
   ctx.body = {
    code: 0,
    message: '删除成功',
    result: ''
   }
  }
 }
 async remove_2(ctx) {
  const res = await removeGoods(ctx.params.id)
  if (res) {
   ctx.body = {
    code: 0,
    message: '下架商品成功',
    result: ''
   }
  } else {
   return ctx.app.emit('error', invalidGoodsID, ctx)
  }
 }
 async restore(ctx) {
  const res = await restoreGoods(ctx.params.id)
  if (res) {
   ctx.body = {
    code: 0,
    message: '上架商品成功',
    result: ''
   }
  } else {
   return ctx.app.emit('error', invalidGoodsID, ctx)
  }
 }
 async findAll(ctx) {
  const { pageNum = 1, pageSize = 10 } = ctx.request.query
  const res = await findGoods(pageNum, pageSize)
  ctx.body = {
   code: 0,
   message: '获取商品成功',
   result: res
  }
 }
}

module.exports = new GoodsController()
